package com.mj.haier.calc.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.mj.haier.calc.domain.AzEncourageCash;
import com.mj.haier.calc.repository.AzEncourageCashRepository;
import com.mj.haier.calc.repository.search.AzEncourageCashSearchRepository;
import com.mj.haier.calc.web.rest.errors.BadRequestAlertException;
import com.mj.haier.calc.web.rest.util.HeaderUtil;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * REST controller for managing AzEncourageCash.
 */
@RestController
@RequestMapping("/api")
public class AzEncourageCashResource {

    private final Logger log = LoggerFactory.getLogger(AzEncourageCashResource.class);

    private static final String ENTITY_NAME = "microCalcAzEncourageCash";

    private final AzEncourageCashRepository azEncourageCashRepository;

    private final AzEncourageCashSearchRepository azEncourageCashSearchRepository;

    public AzEncourageCashResource(AzEncourageCashRepository azEncourageCashRepository, AzEncourageCashSearchRepository azEncourageCashSearchRepository) {
        this.azEncourageCashRepository = azEncourageCashRepository;
        this.azEncourageCashSearchRepository = azEncourageCashSearchRepository;
    }

    /**
     * POST  /az-encourage-cashes : Create a new azEncourageCash.
     *
     * @param azEncourageCash the azEncourageCash to create
     * @return the ResponseEntity with status 201 (Created) and with body the new azEncourageCash, or with status 400 (Bad Request) if the azEncourageCash has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/az-encourage-cashes")
    @Timed
    public ResponseEntity<AzEncourageCash> createAzEncourageCash(@RequestBody AzEncourageCash azEncourageCash) throws URISyntaxException {
        log.debug("REST request to save AzEncourageCash : {}", azEncourageCash);
        if (azEncourageCash.getId() != null) {
            throw new BadRequestAlertException("A new azEncourageCash cannot already have an ID", ENTITY_NAME, "idexists");
        }
        AzEncourageCash result = azEncourageCashRepository.save(azEncourageCash);
        azEncourageCashSearchRepository.save(result);
        return ResponseEntity.created(new URI("/api/az-encourage-cashes/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /az-encourage-cashes : Updates an existing azEncourageCash.
     *
     * @param azEncourageCash the azEncourageCash to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated azEncourageCash,
     * or with status 400 (Bad Request) if the azEncourageCash is not valid,
     * or with status 500 (Internal Server Error) if the azEncourageCash couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/az-encourage-cashes")
    @Timed
    public ResponseEntity<AzEncourageCash> updateAzEncourageCash(@RequestBody AzEncourageCash azEncourageCash) throws URISyntaxException {
        log.debug("REST request to update AzEncourageCash : {}", azEncourageCash);
        if (azEncourageCash.getId() == null) {
            throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull");
        }
        AzEncourageCash result = azEncourageCashRepository.save(azEncourageCash);
        azEncourageCashSearchRepository.save(result);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, azEncourageCash.getId().toString()))
            .body(result);
    }

    /**
     * GET  /az-encourage-cashes : get all the azEncourageCashes.
     *
     * @return the ResponseEntity with status 200 (OK) and the list of azEncourageCashes in body
     */
    @GetMapping("/az-encourage-cashes")
    @Timed
    public List<AzEncourageCash> getAllAzEncourageCashes() {
        log.debug("REST request to get all AzEncourageCashes");
        return azEncourageCashRepository.findAll();
    }

    /**
     * GET  /az-encourage-cashes/:id : get the "id" azEncourageCash.
     *
     * @param id the id of the azEncourageCash to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the azEncourageCash, or with status 404 (Not Found)
     */
    @GetMapping("/az-encourage-cashes/{id}")
    @Timed
    public ResponseEntity<AzEncourageCash> getAzEncourageCash(@PathVariable Long id) {
        log.debug("REST request to get AzEncourageCash : {}", id);
        Optional<AzEncourageCash> azEncourageCash = azEncourageCashRepository.findById(id);
        return ResponseUtil.wrapOrNotFound(azEncourageCash);
    }

    /**
     * DELETE  /az-encourage-cashes/:id : delete the "id" azEncourageCash.
     *
     * @param id the id of the azEncourageCash to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/az-encourage-cashes/{id}")
    @Timed
    public ResponseEntity<Void> deleteAzEncourageCash(@PathVariable Long id) {
        log.debug("REST request to delete AzEncourageCash : {}", id);

        azEncourageCashRepository.deleteById(id);
        azEncourageCashSearchRepository.deleteById(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

    /**
     * SEARCH  /_search/az-encourage-cashes?query=:query : search for the azEncourageCash corresponding
     * to the query.
     *
     * @param query the query of the azEncourageCash search
     * @return the result of the search
     */
    @GetMapping("/_search/az-encourage-cashes")
    @Timed
    public List<AzEncourageCash> searchAzEncourageCashes(@RequestParam String query) {
        log.debug("REST request to search AzEncourageCashes for query {}", query);
        return StreamSupport
            .stream(azEncourageCashSearchRepository.search(queryStringQuery(query)).spliterator(), false)
            .collect(Collectors.toList());
    }

}
